/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package core;

/**
 *
 * @author victor
 */
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.util.HashMap;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

/**
 * Finds all the rooms in a XML file.
 * @author Victor
 */
public class SettingsFinder extends DefaultHandler {
    private HashMap<String,Boolean> hashTag;
    private Actions settingType;
    private String param2Name;
    private String param1Name;
    private boolean enabled;
    public SettingsFinder(){
        super();
        hashTag=new HashMap<String, Boolean>();
        hashTag.put("SETTING", Boolean.FALSE);
        hashTag.put("PARAM1",Boolean.FALSE);
        hashTag.put("PARAM2",Boolean.FALSE);
        param1Name="";
        param2Name="";
        enabled=true;
    }
    @Override
    public void startElement (String uri, String localName,
			      String qName, Attributes attributes)
	throws SAXException
    {
        if (hashTag.containsKey(qName)){
            hashTag.put(qName, Boolean.TRUE);

        }
        if(qName.equals("SETTING")){
            if(attributes.getValue("type")!=null){
                settingType=Actions.stringToAction(attributes.getValue("type"));
                
                
            }
            if(attributes.getValue("enabled")!=null){
                enabled=Boolean.valueOf(attributes.getValue("enabled")).booleanValue();

            }
            else enabled=true;
            System.out.println("Type : "+settingType.getName());
            param1Name=new String();
            param2Name=new String();
        }


    }
    @Override
    public void startDocument(){
        System.out.println("Start document");
    }
    @Override
    public void endDocument(){

        System.out.println("End document1");
        System.out.println("End document2");
    }
    /**
     * Prints any text found in a <ROOM> element.
     * @param chars
     * @param iStart
     * @param iLen
     */
    @Override
    public void characters(char[] chars, int iStart, int iLen) {
        if(hashTag.get("SETTING").equals(Boolean.TRUE)){
            if(hashTag.get("PARAM2").equals(Boolean.TRUE)) param2Name=new String(chars,iStart,iLen);
            else if(hashTag.get("PARAM1").equals(Boolean.TRUE)) param1Name=new String(chars,iStart,iLen);
        }


    }
    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException{
        if(hashTag.containsKey(qName)){
            hashTag.put(qName,Boolean.FALSE);
        }
        if(qName.equals("SETTING")){
            new Setting(settingType, param1Name, param2Name,enabled);
        }
    }
}